package sg.edu.nus.iss.vmcs.store;

/**
 * @author Kyaw Soe Ya
 *
 */
import sg.edu.nus.iss.vmcs.abstraction.IStoreService;
import sg.edu.nus.iss.vmcs.system.ServiceRepository;
import sg.edu.nus.iss.vmcs.util.IObservable;
import sg.edu.nus.iss.vmcs.util.IObserver;

public class StoreItem implements IObservable
{

    private StoreObject content;
    private int quantity;

    public StoreItem(StoreObject content, int quantity)
    {
	this.content = content;
	this.quantity = quantity;
    }

    public StoreObject getContent()
    {
	return content;
    }

    public void setContent(StoreObject ct)
    {
	content = ct;
    }

    public void setQuantity(int quantity)
    {
	this.quantity = quantity;
	notifyObservers();
    }

    public int getQuantity()
    {
	return quantity;
    }

    public void store()
    {
	quantity++;
	notifyObservers();
    }

    public void decrement()
    {
	quantity--;
	if (quantity < 0)
	    quantity = 0;
	notifyObservers();
    }

    public void increment()
    {
	quantity++;
	notifyObservers();
    }

    @Override
    public int hashCode()
    {
	if (this.content != null)
	{
	    return this.content.hashCode();
	} else
	    return super.hashCode();
    }

    @Override
    public boolean equals(Object obj){	
	if(obj != null){
	    StoreItem temp = (StoreItem) obj;   
	    return this.content.equals(temp.getContent());
	}
	else return false;
    }
    
    @Override
    public void attach(IObserver observer)
    {
	// TODO Auto-generated method stub
	StoreService storeService = (StoreService) ServiceRepository
		.GetInstance().getService(IStoreService.class);
	storeService.register(this, observer);

    }

    @Override
    public void detach(IObserver observer)
    {
	// TODO Auto-generated method stub
	StoreService storeService = (StoreService) ServiceRepository
		.GetInstance().getService(IStoreService.class);
	storeService.unRegister(this, observer);

    }

    @Override
    public void notifyObservers()
    {
	// TODO Auto-generated method stub
	StoreService storeService = (StoreService) ServiceRepository
		.GetInstance().getService(IStoreService.class);
	storeService.notifyObservers(this);
    }
}